package com.rightsidetech.http.controller.admin.system;

import com.rightsidetech.http.aspect.Auth;
import com.rightsidetech.http.aspect.Log;
import com.rightsidetech.http.aspect.Token;
import com.rightsidetech.http.base.BaseController;
import com.rightsidetech.system.dtos.request.SysResourceSetGradeReq;
import com.rightsidetech.system.dtos.request.SysResourceUpdateReq;
import com.rightsidetech.system.service.SysResourceService;
import com.rightsidetech.commom.base.ResponseData;
import com.rightsidetech.commom.enums.CodeIdEnum;
import com.rightsidetech.commom.utils.BeanValidator;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.shenyu.client.springmvc.annotation.ShenyuSpringMvcClient;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.validation.ConstraintViolation;
import java.util.List;

/**
 * @author yhr
 * @date 2021/10/20 16:35
 */

@RestController
@RequestMapping("/admin/sys_resource")
@ShenyuSpringMvcClient(path = "/admin/sys_resource/**")
@Api(tags = "admin:后台菜单")
public class SysResourceController extends BaseController {
    @Resource
    private SysResourceService sysResourceService;

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/saveResources")
    @ApiOperation("admin:批量添加")
    public ResponseData saveAddResources(@RequestBody List<SysResourceUpdateReq> sysAddResourcesModelList) throws Exception {
        if(null == sysAddResourcesModelList || sysAddResourcesModelList.size() <= 0){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }
        return sysResourceService.saveAddResources(sysAddResourcesModelList);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("setGrade")
    @ApiOperation("设置菜单级别")
    public ResponseData setGrade(@RequestBody List<SysResourceSetGradeReq> list) throws Exception{
        if(null == list || list.size() <= 0){
            return ResponseData.fail(CodeIdEnum.EMPTY_DATA);
        }

        for (SysResourceSetGradeReq req:list) {
            ConstraintViolation validate = BeanValidator.validate(req);
            if (validate != null) {
                return new ResponseData(CodeIdEnum.ParameterMalformed.getValue(), validate.getMessage());
            }
        }

        return sysResourceService.setGreade(list);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @PostMapping("/list/del")
    @ApiOperation("admin:批量删除")
    public ResponseData delList(@RequestBody List<Long> list) throws Exception {
        return sysResourceService.delList(list);
    }

    @Log
    @Token
    @Auth(bizType = Auth.OPERATOR)
    @GetMapping("/list")
    @ApiOperation("admin:菜单列表")
    public ResponseData findAllList(Long parentId) throws Exception {
        return sysResourceService.findAllList(parentId);
    }

    @Log
    @Token
    @GetMapping("/getList")
    @ApiOperation("admin:查询当前账户的所有权限")
    public ResponseData getAllSysResource() throws Exception {
        Long loginInfoId = Long.parseLong(getAttribute());

        return sysResourceService.getAllSysResource(loginInfoId);
    }

}
